package wypozyczalnia.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import wypozyczalnia.domain.Person;
import wypozyczalnia.forms.EditPersonForm;
import wypozyczalnia.forms.EditcarForm;
import wypozyczalnia.forms.RentForm;
import wypozyczalnia.service.PersonService;
import wypozyczalnia.service.PhotoService;
import wypozyczalnia.utils.FileUploader;
//import wypozyczalnia.service.CarService;

@Controller
@RequestMapping(value = "/adminEditorCars/*")
public class AdminEditorCarsController {

	private final Logger logger = Logger.getLogger(ProfileController.class);
//	@Autowired
//	CarService carService;

	@Autowired
	PhotoService photoService;
	
	@Autowired
	FileUploader fileUploader;
	
	@Autowired
	PersonService personService;
	


	@RequestMapping(value = "adminListCars.do")
	public String list(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model) {
//		ArrayList<Car> cars = new ArrayList<Car>(carService.getallCars());
		logger.info("adminListCars");
//		model.addAttribute("cars", cars);
		return "adminListCars";
	}
	
	@RequestMapping(value = "adminListPersons.do")
	public String listPersons(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model) {
		
		ArrayList<Person> persons = new ArrayList<Person>(personService.getAllPersons());
		logger.info("adminListPersons");
		model.addAttribute("persons", persons);
		return "adminListCars";
	}

	@RequestMapping(value = "adminAddCar.do", method = RequestMethod.GET)
	public String addCar(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model) {
		logger.info("adminAddCar with method GET");

		return "adminAddCar";

	}
	@RequestMapping(value = "adminAddPerson.do", method = RequestMethod.GET)
	public String addPerson(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, EditPersonForm editPersonForm) {
		logger.info("adminAddPerson with method GET");

		return "adminAddPerson";

	}

	@RequestMapping(value = "adminAddPerson.do", method = RequestMethod.POST)
	public String addPersonAction(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			Model model, EditPersonForm editPersonForm) {
		logger.info("adminAddPerson with method POST");
		String name = editPersonForm.getName();
		String surname = editPersonForm.getSurname();
		String desc = editPersonForm.getDescription();
//		String birthDate = editPersonForm.getBirthDate();
//		String nationality = editPersonForm.getNationality();
//		personService.create(new Person(name, surname, desc, birthDate, nationality));
//		carService.create(new Car(brand, modelCar, year, color, registNumber,
//				desc, true));

		return "adminAddPerson";

	}

	@RequestMapping(value = "adminEditCar.do", method = RequestMethod.GET)
	public String editCar(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, RentForm rentForm,
			EditcarForm editcarForm) {
		logger.info("adminEditCar with method GET");
//		Car selectedCar = carService.getCarById(rentForm.getId());
//		editcarForm.setBrand(selectedCar.getBrand());
//		editcarForm.setModel(selectedCar.getModel());
//		editcarForm.setColor(selectedCar.getColor());
//		editcarForm.setDesc(selectedCar.getDescription());
//		editcarForm.setRegistNumber(selectedCar.getRegistration_number());
//		editcarForm.setYear(selectedCar.getYear());
//		model.addAttribute("car", selectedCar);
//		model.addAttribute("id", selectedCar.getId());
		return "adminEditCar";

	}

	@RequestMapping(value = "adminEditCar.do", method = RequestMethod.POST)
	public String changedCar(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, EditcarForm editcarForm) {
		logger.info("adminEditCar with method POST");

//		Car car = carService.getCarById(editcarForm.getId());
//		car.setBrand(editcarForm.getBrand());
//		car.setModel(editcarForm.getModel());
//		car.setColor(editcarForm.getColor());
//		car.setDescription(editcarForm.getDesc());
//		car.setRegistration_number(editcarForm.getRegistNumber());
//		car.setYear(editcarForm.getYear());

//		carService.update(car);
//		ArrayList<Car> cars = new ArrayList<Car>(carService.getallCars());
//		logger.info("adminListCars");
//		model.addAttribute("cars", cars);
		return "adminListCars";

	}

	@RequestMapping(value = "adminDeleteCar.do", method = RequestMethod.GET)
	public String deleteCar(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, RentForm rentForm,
			EditcarForm editcarForm) {
		logger.info("adminDeleteCar with method GET");

//		carService.deleteById(rentForm.getId());
//		ArrayList<Car> cars = new ArrayList<Car>(carService.getallCars());
//		logger.info("adminListCars");
//		model.addAttribute("cars", cars);
		return "adminListCars";

	}
	
	
	@RequestMapping(value = "adminUploadgallerytab.do", method = RequestMethod.POST)
	public void upload(HttpSession session, HttpServletRequest request,
			HttpServletResponse response,Long id) throws IOException {
		try {
			byte [] image = fileUploader.getFileContent(request);

//			Photo ph=new Photo(image, false,carService.getCarById(id.intValue()));
//			System.out.println(ph.getPhoto().length+"byte");
//			photoService.create(ph);
		} catch (FileUploadException e) {
			fileUploader.respond(response, e.getMessage());
		}
		fileUploader.respond(response, "Wczytano!");
	}

}
